﻿using Alexinea.Autofac.Extensions.DependencyInjection;
using Autofac;
using MT.Common.Cache;
using MT.Common.Helper;
using MT.DAL.Abstract;
using MT.DAL.Concrete;
using MT.Models.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.DependencyInjection;
using User = MT.Job.Core.Models.User;

namespace MT.Job.Core.Extensions
{
    public static class ServicesExtensions
    {
        public static ContainerBuilder ConfigureSelf(this ContainerBuilder builder)
        {
            var services = new ServiceCollection();

            services.AddDbContext<AppDbContext>(options => options.UseSqlServer(ConfigHelper.GetSection("ConnectionStrings", "MedicalBeautyDatabase")), ServiceLifetime.Scoped);
            // 缓存
            services.AddSingleton(typeof(ICacheService), new RedisCacheService(new RedisCacheOptions
            {
                Configuration = ConfigHelper.GetSection("CacheConfiguration", "Redis_ConnectionString"),
                InstanceName = ConfigHelper.GetSection("CacheConfiguration", "Redis_InstanceName")

            }, 0));

            services.AddSingleton<IMemoryCache>(factory =>
            {
                var cache = new MemoryCache(new MemoryCacheOptions());
                return cache;
            });

            // register appsettings.json
            services.Configure<User>("UserConfig", Settings.Instance.Configuration.GetSection("User"));
            builder.Populate(services);
            // register services repositories
            builder.RegisterType<ServiceConstructorParams>().As<IServiceConstructorParams>();
           
            
            
            //builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly)
            //       .Where(t => t.Name.EndsWith("Repository"))
            //       .AsImplementedInterfaces();
            //builder.RegisterAssemblyTypes(typeof(UserService).Assembly)
            //     .Where(t => t.Name.EndsWith("Service"))
            //     .AsImplementedInterfaces();

            //builder.RegisterAssemblyTypes(typeof(MedicalService).Assembly)
            //      .Where(t => t.Name.EndsWith("Service"))
            //     .AsImplementedInterfaces();

            //  builder.RegisterGeneric(typeof(MedicalService<>)).As(typeof(IMedicalService<>)).InstancePerDependency();
            return builder;
        }
    }
}
